--[[
Author: wangxiaoyan
Date: 2020-10-22 20:52:51
LastEditTime: 2020-11-12 16:52:40
FilePath: \Lua\View\CharactorUI\UICharactorInfoHeroDetail.lua
--]]

require 'View/UIBase/UIComponent/UIComponentBase'

UICharactorInfoHeroDetail = Class("UICharactorInfoHeroDetail",UIComponentBase)
local _M = UICharactorInfoHeroDetail

function _M:ctor(obj,parent)
    self.fguiObj = obj
    self.parent = parent
    self:Create()
end

function _M:OnCreate()
    self.skillData = nil

    self.fguiObj.visible = false

    self.qualityFrame = self:GetChild("break2/frame")
    self.qualityIcon = self:GetChild("break2/icon")
    self.qualityNumIcon = self:GetChild("break2/numIcon")
    self.staegeController = self:GetController("jinjie",self:GetChild("break2"))
    self.maxController = self:GetController("LR",self:GetChild("break2"))

    self.qualityFrame_2 = self:GetChild("break1/frame")
    self.qualityIcon_2 = self:GetChild("break1/icon")
    self.qualityNumIcon_2 = self:GetChild("break1/numIcon")
    self.staegeController_2 = self:GetController("jinjie",self:GetChild("break1"))
    self.maxController_2 = self:GetController("LR",self:GetChild("break1"))

    self.attrHPSp = self:GetChild("attr0/icon")
    self.attrHP = self:GetChild("attr0/num")
    self.attrHPUP = self:GetChild("attr0/upNum")
    self.attrACKSp = self:GetChild("attr1/icon")
    self.attrACK = self:GetChild("attr1/num")
    self.attrACKUP = self:GetChild("attr1/upNum")
    self.attrDEFSp = self:GetChild("attr2/icon")
    self.attrDEF = self:GetChild("attr2/num")    
    self.attrDEFUP = self:GetChild("attr2/upNum")
    self.uptxt = self:GetChild("upTitle")
    self.unLocktxt = self:GetChild("unlockTitle")

    --[[self.skills = {}
    for i = 1, 4 do
        local skill = {}
        skill.activeSkill = {}
        skill.upSkill = {}
        table.insert(self.skills,skill)
        skill.activeSkill.root = self:GetChild("skill"..i)
        skill.activeSkill.icon = self:GetChild("mask/icon",skill.activeSkill.root)
        skill.activeSkill.num = self:GetChild("title",skill.activeSkill.root)
        skill.upSkill.root = self:GetChild("upSkill"..i)
        skill.upSkill.icon = self:GetChild("mask/icon",skill.upSkill.root)
        skill.upSkill.num = self:GetChild("title",skill.upSkill.root)
    end--]]

    --self.toggleDynamicList = self.parent:SetDynamicList(self:GetChild("starList"),self.RefreshDynamicItem,self)
    self.parent:RegisterOnClick(self:GetChild("btnBack"),self.ClickExit,self)
end

function _M:OnComEnable()
    
end

function _M:Show(heroData)
    self.heroData = heroData
    self:InitSkillData()
    self:OnEnable()
end

function _M:OnEnable()
    self.fguiObj.visible = true
    
    self:RefreshAttr()
    --self:RefreshSkill()
    -- TODO WG 最大星级取值？
    --self.toggleDynamicList.numItems = 6
    local star2 = logicMgr.HeroManager.starToQuality(self.heroData.star)
    self:SetStar(star2,self.qualityFrame_2,self.qualityIcon_2,self.staegeController_2,self.qualityNumIcon_2,self.maxController_2)
    local star =logicMgr.HeroManager.getNextQuality(self.heroData.star)
    self:SetStar(star,self.qualityFrame,self.qualityIcon,self.staegeController,self.qualityNumIcon,self.maxController)
end

function _M:SetStar(star,qualityFrame,qualityIcon,staegeController,qualityNumIcon,maxController)
    local config = G.dataTable.t_iconframe[tostring(star)]
    local isFrame = config.f_Ornaments ~= ""
    qualityFrame.visible = isFrame
    local atlas = "ui://CharactorUI/"
    if isFrame then
        qualityFrame.url = atlas..config.f_QualityOrnaments
    end
    if config.f_QualityText~= "" then
        qualityIcon.url = atlas..config.f_QualityText
        staegeController.selectedIndex = 1;
    else
        staegeController.selectedIndex = 0;
    end
    qualityNumIcon.url = atlas..config.f_QualityIcon
    if config.f_Id > 10 then
        staegeController.selectedIndex = 0;
        maxController.selectedIndex = star-10;
    else
        maxController.selectedIndex = 0;
    end
end

function _M:OnDisable()
    self.heroData = nil
    self.fguiObj.visible = false
end

function _M:OnDestroy()
    self.skillData = nil
end

function _M:InitSkillData()
    self.skillData={}
    self.showStar = logicMgr.HeroManager.getNextStar(self.heroData.star)
    local heroConfig=G.dataTable.t_hero[tostring(self.heroData.partnerModelId)];
    for i = 1, 4 do
        local tempSkillInfo=logicMgr.SkillManager.getSkillInfoConfig(heroConfig["f_HeroSkill"..i])
        if tempSkillInfo then
            local skillList = decodeJsonStrToTabel(tempSkillInfo.f_SkillList);
            local skillupStar= decodeJsonStrToTabel(tempSkillInfo.f_LevelRequireRank);
            local skillupLevel=decodeJsonStrToTabel(tempSkillInfo.f_SkillLevel);
            for j = 1, #skillupStar do
                if(self.showStar==skillupStar[j]/1000)then
                    local skillUpData={}
                    skillUpData.id=skillList[j]
                    skillUpData.level=skillupLevel[j]
                    table.insert( self.skillData,skillUpData);
                end
            end
        end
    end
end

function _M:RefreshDynamicItem(index,itemObj)
    local controller = self:GetController("cstar",itemObj)
    controller.selectedIndex = index <= self.showStar*0.1 and 1 or 0
end

function _M:RefreshAttr()
    local function getCfg(id)
        local lastID=550150
        local info=dataTable.getRow("t_herorank", id)
        if table.getn(info)==0 then
            info=dataTable.getRow("t_herorank", lastID)
        end
        return info
    end
    self:SetIcon(self.attrHPSp,"attr"..EnumConst.FighterAttributeEnum.HP,EnumConst.ItemShowType.ICON)
    self:SetIcon(self.attrACKSp,"attr"..EnumConst.FighterAttributeEnum.ATK,EnumConst.ItemShowType.ICON)
    self:SetIcon(self.attrDEFSp,"attr"..EnumConst.FighterAttributeEnum.DEF,EnumConst.ItemShowType.ICON)

    local cfgId = toNumber(self.hero["partnerModelId"]) * 1000 + self.hero.star
    local cfgInfo = getCfg(cfgId)
    local cfgID2 = cfgInfo.f_HeroStarNext
    local cfgInfo2 = getCfg(cfgID2)


    local baseData = clone(self.heroData)
    logicMgr.HeroManager.processHeroBaseAttr(baseData,self.heroData.level,self.heroData.star)
    local attbase = baseData.allAttribute[EnumConst.AttributeTypeEnum.BASE]
    self.attrHP.text = attbase[EnumConst.FighterAttributeEnum.HP]
    self.attrACK.text = attbase[EnumConst.FighterAttributeEnum.ATK]
    self.attrDEF.text = attbase[EnumConst.FighterAttributeEnum.DEF]

    logicMgr.HeroManager.processHeroBaseAttr(baseData,self.heroData.level,cfgInfo2.f_HeroStar)
    local attup = baseData.allAttribute[EnumConst.AttributeTypeEnum.BASE]
    self.attrHPUP.text = attup[EnumConst.FighterAttributeEnum.HP]
    self.attrACKUP.text = attup[EnumConst.FighterAttributeEnum.ATK]
    self.attrDEFUP.text = attup[EnumConst.FighterAttributeEnum.DEF]
end

function _M:RefreshSkill()
    local skillDataCount = #self.skillData
    self.uptxt.visible = false
    self.unLocktxt.visible = false;
    for index, skill in ipairs(self.skills) do
        local isShow = index <= skillDataCount
        if isShow then
            local skillData = self.skillData[index]
            local config = logicMgr.SkillManager.getSkillInfoConfig(skillData.id);
            local isActive = skillData.level == 1
            self.unLocktxt.visible = isActive
            skill.activeSkill.root.visible = isActive
            skill.upSkill.root.visible = not isActive
            self.uptxt.visible = not isActive
            local icon = isActive and skill.activeSkill.icon or skill.upSkill.icon
            local num = isActive and skill.activeSkill.num or skill.upSkill.num
            self:SetIcon(icon,config.f_SkillIcon,EnumConst.ItemShowType.SKILL)
            self:SetText(num,skillData.level)
        else
            skill.activeSkill.root.visible = false
            skill.upSkill.root.visible = false
        end
    end
end

function _M:ClickExit()
    self:Disable()
end